Barcode Xpress for Java v13.1 - Updated
Tutorial: Create Your First Project
User Guide > Getting Started > Tutorial: Create Your First Project

This step-by-step tutorial will guide you to build a complete application that analyzes a Code 39 barcode from an image and outputs the value, type, location and confidence to the terminal.

Prerequisites

Before running this tutorial, a license for Barcode Xpress for Java should already be installed.

This demo works both on Windows and Linux platforms.

Create BxJavaDemo

  1. Create a folder, BxJavaDemo, inside Accusoft/BarcodeXpressJava13-64/samples/.
    Then, create a sub folder, src/main/java, inside BxJavaDemo.
  2. Create a new file, BxJavaDemo.java, in BxJavaDemo/src/main/java/ and open it in your favorite text editor.
  3. Import barcodexpress and the other packages (as shown below) and define the main class BxJavaDemo and the main() method:
    Copy Code
    import com.accusoft.barcodexpress.*;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
     
    public class BxJavaDemo
    {
     public static void main(String[] args)
        {
         …
     }
    }
    
  4. Inside the main method, initialize the barcodexpress instance and enter the license information:
    Copy Code
    BarcodeXpress barcodeXpress = new BarcodeXpress();
    BarcodeReader barcodeReader = barcodeXpress.getReader();
        
    // The BarcodeXpress.setSolutionName, BarcodeXpress.setSolutionKey and possibly the BarcodeXpress.setOemLicenseKey methods must be
    // called to distribute the runtime.  Note that the SolutionName, SolutionKey and OEMLicenseKey values shown below are only for example.
    // BarcodeXpress.setSolutionName("YourSolutionName");
    // BarcodeXpress.setSolutionKey(1234,1234,1234,1234);
    // BarcodeXpress.setOemLicenseKey("2.0.YourOEMLicenseKeyGoesHere");
    
  5. Get the image file name from the command line arguments:
    Copy Code
    if (0 == args.length)
    {
     System.out.println("Please specify input image(s).");
     return;
    }
     
    String inputFilePath = args[0];
    
  6. Setup the barcode reader to read only Code39 barcodes. If you want to search for other types, see the supported barcode types and use the type that suits your requirements.
    Set the barcode type:
    Copy Code
    BarcodeType barcodeType = BarcodeType.CODE39;
    barcodeReader.setBarcodeTypes(new BarcodeType[] { BarcodeType.CODE39});
    
  7. Add a useful print message:
    Copy Code
    System.out.println("Scanning file '" + inputFilePath + "' for " + barcodeType.toString() + " barcodes.\n");
    
  8. Before calling the analyze method, use a try-catch block as analyze does throw exceptions:
    Copy Code
    try {
     …
    }
    catch (BarcodeException bEx) {
     System.out.println(bEx.getMessage());
    }
    catch (Exception ex) {
     System.out.println(ex.getMessage());
    }
    
  9. Inside that block, open and read the input file into bufferedImage:
    Copy Code
    File inputFile = new File(inputFilePath);
    BufferedImage bufferedImage = ImageIO.read(inputFile);
    
  10. Call analyze and print the resulting information:
    Copy Code
    Result[] results = barcodeReader.analyze(bufferedImage);
     
    System.out.println(results.length + " barcodes found. \n");
     
    for (int i = 0; i < results.length; i++) {
     System.out.println("#" + (i+1));
     System.out.println("Barcode value = '" + results[i].getValue() + "'");
     System.out.println("Barcode type = '" + results[i].getType() + "'\n");
    }
    
  11. Here’s the full listing of the program:
    Copy Code
    import com.accusoft.barcodexpress.*;
    import javax.imageio.ImageIO;
    import java.awt.image.BufferedImage;
    import java.io.File;
     
    public class BxJavaDemo
    {
     public static void main(String[] args)
        {
         BarcodeXpress barcodeXpress = new BarcodeXpress();
         BarcodeReader barcodeReader = barcodeXpress.getReader();
        
         // The SetSolutionName, SetSolutionKey and possibly the SetOEMLicenseKey method must be
         // called to distribute the runtime.  Note that the SolutionName, SolutionKey and
         // OEMLicenseKey values shown below are only examples.
         // BarcodeXpress.setSolutionName("YourSolutionName");
         // BarcodeXpress.setSolutionKey(1234,1234,1234,1234);
         // BarcodeXpress.setOemLicenseKey("2.0.YourOEMLicenseKeyGoesHere");
           
            if (0 == args.length)
            {
             System.out.println("Please specify input image(s).");
             return;
         }
        
            String inputFilePath = args[0];
        
            BarcodeType barcodeType = BarcodeType.CODE39;
         barcodeReader.setBarcodeTypes(new BarcodeType[] { barcodeType });
     
         System.out.println("Scanning file '" + inputFilePath + "' for " + barcodeType.toString() + " barcodes.\n");
     
         try {
             File inputFile = new File(inputFilePath);
             BufferedImage bufferedImage = ImageIO.read(inputFile);
     
             Result[] results = barcodeReader.analyze(bufferedImage);
     
             System.out.println(results.length + " barcodes found. \n");
     
             for (int i = 0; i < results.length; i++) {
                 System.out.println("#" + (i+1));
                 System.out.println("Barcode value = '" + results[i].getValue() + "'");
                 System.out.println("Barcode type = '" + results[i].getType() + "'\n");
             }
         }
         catch (BarcodeException bEx) {
             System.out.println(bEx.getMessage());
         }
         catch (Exception ex) {
             System.out.println(ex.getMessage());
         }
     }
    }
    
  12. Compile BxJavaDemo.
    Run the following commands in the console in the BxJavaDemo folder Accusoft/BarcodeXpressJava13-64/samples/BxJavaDemo:
    Linux
    Copy Code
    javac -cp ".:src/main/java:../../bin/barcodexpressjava-13.1.jar" src/main/java/BxJavaDemo.java
    
    Windows
    Copy Code
    javac -cp ".;src/main/java;../../bin/barcodexpressjava-13.1.jar" src/main/java/BxJavaDemo.java
    
  13. The BxJavaDemo.java file compiles and creates the Accusoft/BarcodeXpressJava13-64/samples/BxJavaDemo/src/main/java/BxJavaDemo.class class file.
  14. Run the demo. Specify an input image with a Code39 barcode from the images folder of the Barcode Xpress installation:
    Linux
    Copy Code
    java -cp ".:src/main/java:../../bin/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
    
    Windows
    Copy Code
    java -cp ".;src/main/java;../../bin/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
    
  15. You should see following output:
    Copy Code
    Scanning file '../images/Barcode-Realworld-Code39-3.bmp' for CODE39 barcodes.
     
    2 barcodes found.
     
    #1
    Barcode value = '1202804105'
    Barcode type = 'CODE39'
     
    #2
    Barcode value = '95905568'
    Barcode type = 'CODE39'
    

Improve BxJavaDemo by Adding Maven Support

  1. We can improve our BxJavaDemo by adding Maven support. This allows you to fetch BX artifacts from the Accusoft Public Maven repository and also simplify compilation and packaging.
    First, you need to create the pom.xml file in the Accusoft/BarcodeXpressJava13-64/samples/BxJavaDemo folder and paste following content:
    Copy Code
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     
     <groupId>com.accusoft.barcodexpress.samples</groupId>
     <artifactId>BxJavaDemo</artifactId>
     <version>1.0</version>
     
     <repositories>
         <repository>
             <releases>
                 <enabled>true</enabled>
                 <updatePolicy>always</updatePolicy>
                 <checksumPolicy>warn</checksumPolicy>
             </releases>
             <snapshots>
                 <enabled>false</enabled>
                 <updatePolicy>never</updatePolicy>
                 <checksumPolicy>fail</checksumPolicy>
             </snapshots>
             <id>accusoft-public-releases</id>
             <name>Accusoft Maven Releases</name>
             <url>http://mvn.accusoft.com</url>
             <layout>default</layout>
         </repository>
     </repositories>
     
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>2.3.2</version>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                 </configuration>
             </plugin>
         </plugins> 
        </build>
     
     <dependencies>
         <dependency>
             <groupId>com.accusoft.barcodexpress</groupId>
             <artifactId>barcodexpressjava</artifactId>
             <version>13.1</version>
         </dependency>
     </dependencies>
     
    </project>
    
  2. Run following commands:
    Copy Code
    mvn clean package dependency:copy-dependencies
    
    It will request maven to clean distribution, compile and package BxJavaDemo into a jar (all needed files will be present in "target" folder) with copy dependencies (in our case it is a BX jar file) from the Maven local repository (Maven caches it there from the public remote repository) to the "target/dependency/" folder.
  3. Now, run the demo:
    Linux
    Copy Code
    java -cp "target/BxJavaDemo-1.0.jar:target/dependency/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
    
    Windows
    Copy Code
    java -cp "target/BxJavaDemo-1.0.jar;target/dependency/barcodexpressjava-13.1.jar" BxJavaDemo ../images/Barcode-Realworld-Code39-3.bmp
    
  4. You should see the output to be the same as before. You’re good to go!